#include "mygui.h"
#include "ui_mygui.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QString>
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <sstream>
#include <math.h>
using namespace std;

#include "filesystem.h"
#include <ui_meshedge.h>
#include <ui_meshface.h>

#include "../model/model.h"
#include "../attrib/attribmanager.h"


//-------------------------------------------------------------------------
MyGUI::MyGUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyGUI)
{
  ui->setupUi(this);
  ui->widget_2->setMyUI( this );
  // creates and setup the dialog responsible for the grid and snap options
  m_gridDialog = new QDialog;
  m_gridDlg.setupUi( m_gridDialog );
  // creates and setup the dialog responsible for the points
  m_insertPoint = new QDialog;
  m_insertPnt.setupUi( m_insertPoint );
  // creates and setup the dialog responsible for the lines
  m_insertLine = new QDialog;
  m_insertLn.setupUi( m_insertLine );
  // creates and setup the dialog responsible for the rectangles
  m_insertRec = new QDialog;
  m_insertRc.setupUi ( m_insertRec );
  // creates and setup the dialog responsible for the attributes management
  m_attrDialog = new QDialog;
  m_attrDlg.setupUi( m_attrDialog );
  m_attribManager = new AttribManager();
  int npa = m_attribManager->getNumberOfPrototypes();
  for( int i=0; i<npa; i++ )
  {
    QString name( m_attribManager->getPrototypeNameByIndex( i ).c_str() );
    m_attrDlg.comboBox->addItem( name );
  }
  // define the connection slot for the buttons at the attribute maneger dialog
  connect(m_attrDlg.applyButton,    SIGNAL(clicked()), this, SLOT(applyAttributeClicked()));
  connect(m_attrDlg.removeButton,   SIGNAL(clicked()), this, SLOT(removeAttributeClicked()));
  connect(m_attrDlg.cancelButton,   SIGNAL(clicked()), this, SLOT(cancelAttributeClicked()));
  connect(m_attrDlg.createButton,   SIGNAL(clicked()), this, SLOT(createAttributeClicked()));
  connect(m_attrDlg.treeWidget,     SIGNAL(itemSelectionChanged()), this, SLOT(displayAttribProperties()));
  connect(m_attrDlg.tableWidget,    SIGNAL(cellChanged(int,int)),   this, SLOT(updateAttributeValues(int,int)));
  connect(m_attrDlg.horizSlider,    SIGNAL(valueChanged(int)),      this, SLOT(updateAtribSymbolSize(int)));
  connect(m_insertPnt.okButton,     SIGNAL(clicked()), this, SLOT(okInsertPointClicked()));
  connect(m_insertPnt.cancelButton, SIGNAL(clicked()), this, SLOT(cancelInsertPointClicked()));
  connect(m_insertPnt.applyButton,  SIGNAL(clicked()), this, SLOT(applyInsertPointClicked()));
  connect(m_insertLn.okButton,      SIGNAL(clicked()), this, SLOT(okInsertLineClicked()));
  connect(m_insertLn.cancelButton,  SIGNAL(clicked()), this, SLOT(cancelInsertLineClicked()));
  connect(m_insertLn.applyButton,   SIGNAL(clicked()), this, SLOT(applyInsertLineClicked()));
  connect(m_insertRc.okButton,      SIGNAL(clicked()), this, SLOT(okInsertRecClicked()));
  connect(m_insertRc.cancelButton,  SIGNAL(clicked()), this, SLOT(cancelInsertRecClicked()));
  connect(m_insertRc.applyButton,   SIGNAL(clicked()), this, SLOT(applyInsertRecClicked())); 
  // initializes the application in move option
  this->on_actionToolButton_2_triggered();
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
MyGUI::~MyGUI()
{
  delete ui;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::on_actionOpen_triggered()
{
  cout << ".NAO encontrou ARQUIVO" << endl;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::on_actionSave_triggered()
{
  // checks if it is in the mesh MODE, by checking if the mesh button is enable
  string text = "\nFile created with success!\n";
  bool test = ui->actionToolButton_13->isChecked();
  if( test )
  {
    string extXML( ".xml" );
    string extMATLAB( ".m" );
    QFileDialog::Options options;
    QString selectedFilter;
    QString fileName = QFileDialog::getSaveFileName(this,
      ("Save mesh file"),
      "filename",
      ("Befe++ file (*.xml);;Matlab files (*.m);;All Files (*)"),
      &selectedFilter,
      options);
    std::string fname = fileName.toLocal8Bit().constData();
    FILE* pFile = fopen( fname.c_str(), "w" );
    Model* pModel = ui->widget_2->getModel();
    bool status = false;

    FileSystem fileSystem;
    if( fname.rfind( extMATLAB ) != string::npos )
    {
      status = fileSystem.write_MATLAB_File( pFile, pModel );
    }
    if( fname.rfind( extXML ) != string::npos )
    {
      status = fileSystem.write_XML_File( pFile, pModel );
    }
    if( !status )
    {
      text = "\nIt was not possible to save the file!\n";
    }
  }
  else
  {
    text = "\nIt was not possible to save the file!\n\nPlease, press the MESH button.\n";
  }
  QString nameMessage(text.c_str());
  QMessageBox msgBox;
  msgBox.setText(nameMessage);
  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  msgBox.setDefaultButton(QMessageBox::Ok);
  int ret = msgBox.exec();
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::on_actionVertex_triggered()
{
  bool isChecked = ui->actionVertex->isChecked();
  if( isChecked )
    ui->widget_2->setTopolViewFlag( 1, true );
  else
    ui->widget_2->setTopolViewFlag( 1, false );
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::on_actionEdge_triggered()
{
  bool isChecked = ui->actionEdge->isChecked();
  if( isChecked )
    ui->widget_2->setTopolViewFlag( 2, true );
  else
    ui->widget_2->setTopolViewFlag( 2, false );
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::on_actionFace_triggered()
{
  bool isChecked = ui->actionFace->isChecked();
  if( isChecked )
    ui->widget_2->setTopolViewFlag( 3, true );
  else
    ui->widget_2->setTopolViewFlag( 3, false );
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::on_actionAttributes_triggered()
{
  bool isChecked = ui->actionAttributes->isChecked();
  if( isChecked )
    ui->widget_2->setTopolViewFlag( 0, true );
  else
    ui->widget_2->setTopolViewFlag( 0, false );
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_1_triggered()
{
  ui->actionToolButton_1->setChecked(true);
  ui->actionToolButton_2->setChecked(false);
  ui->actionToolButton_3->setChecked(false);
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(false);
  ui->actionToolButton_6->setChecked(false);
  // set user action
  ui->widget_2->setUserOperation( 0, Qt::ArrowCursor );
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_2_triggered()
{
  ui->actionToolButton_1->setChecked(false);
  ui->actionToolButton_2->setChecked(true);
  ui->actionToolButton_3->setChecked(false);
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(false);
  ui->actionToolButton_6->setChecked(false);
  // set user action
  ui->widget_2->setUserOperation( 1, Qt::OpenHandCursor );
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_3_triggered()
{
  ui->actionToolButton_1->setChecked(false);
  ui->actionToolButton_2->setChecked(false);
  ui->actionToolButton_3->setChecked(true);
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(false);
  ui->actionToolButton_6->setChecked(false);
  // set user action
  ui->widget_2->setUserOperation( 2, Qt::SizeAllCursor );
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_4_triggered()
{
  ui->actionToolButton_1->setChecked(false);
  ui->actionToolButton_2->setChecked(false);
  ui->actionToolButton_3->setChecked(false);
  ui->actionToolButton_4->setChecked(true);
  ui->actionToolButton_5->setChecked(false);
  ui->actionToolButton_6->setChecked(false);    

  bool bpress = ui->actionToolButton_14->isChecked();
  if( bpress )  
  {  
    
	Ui::InsertPoint pnt; 
	QDialog *Point = new QDialog;
	pnt.setupUi(Point); 

	double dx = 0.0;
    double dy = 0.0;
	double dtol = 0.0;  

	m_insertPnt.lineEdit->  clear();
	m_insertPnt.lineEdit_2->clear();
	m_insertPnt.lineEdit_3->clear();
	m_insertPoint->show();

  }

   // returns to the selection mode
  this->on_actionToolButton_2_triggered();
  this->on_actionToolButton_1_triggered();
    
  // set user action
  ui->widget_2->setUserOperation( 3, Qt::CrossCursor );
  
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_5_triggered()
{
  ui->actionToolButton_1->setChecked(false);
  ui->actionToolButton_2->setChecked(false);
  ui->actionToolButton_3->setChecked(false);
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(true);
  ui->actionToolButton_6->setChecked(false);

  bool bpress = ui->actionToolButton_14->isChecked();
  if( bpress )  
  { 
    Ui::InsertLine ln; 
	QDialog *Line = new QDialog;
	ln.setupUi(Line); 

	double dx1 = 0.0;
    double dy1 = 0.0;
	double dx2 = 0.0;
    double dy2 = 0.0;
	double dtol = 0.0;  

    m_insertLn.lineEdit->  clear();
	m_insertLn.lineEdit_2->clear();
	m_insertLn.lineEdit_3->clear();
	m_insertLn.lineEdit_4->clear();
	m_insertLn.lineEdit_5->clear();
	
	m_insertLine->show();
  }  

   // returns to the selection mode
  this->on_actionToolButton_2_triggered();
  this->on_actionToolButton_1_triggered();
	  
	  // set user action
  ui->widget_2->setUserOperation( 4, Qt::CrossCursor );
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_6_triggered()
{
  ui->actionToolButton_1->setChecked(false);
  ui->actionToolButton_2->setChecked(false);
  ui->actionToolButton_3->setChecked(false);
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(false);
  ui->actionToolButton_6->setChecked(true);

  bool bpress = ui->actionToolButton_14->isChecked();
  if( bpress )  
  { 
    Ui::InsertRec rec; 
	QDialog *Rec = new QDialog;
	rec.setupUi(Rec); 

	double dx1 = 0.0;
    double dy1 = 0.0;
	double dx2 = 0.0;
    double dy2 = 0.0;
	double dtol = 0.0;  
	
    m_insertRc.lineEdit->  clear();
	m_insertRc.lineEdit_2->clear();
	m_insertRc.lineEdit_3->clear();
	m_insertRc.lineEdit_4->clear();
	m_insertRc.lineEdit_5->clear();
	
	m_insertRec->show();
  }
   // returns to the selection mode
   this->on_actionToolButton_2_triggered();
   this->on_actionToolButton_1_triggered();

   // set user action 
   ui->widget_2->setUserOperation( 5, Qt::CrossCursor );
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_7_triggered()
{
  ui->actionToolButton_1->setChecked(false);
  ui->actionToolButton_2->setChecked(false);
  ui->actionToolButton_3->setChecked(false);
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(false);
  ui->actionToolButton_6->setChecked(false);
  // set user action
  ui->widget_2->setUserOperation( 6, Qt::ArrowCursor );
  if( ui->widget_2->hasUndoing() )
    ui->actionToolButton_7->setEnabled(true);
  else
    ui->actionToolButton_7->setEnabled(false);
  if( ui->widget_2->hasRedoing() )
    ui->actionToolButton_8->setEnabled(true);
  else
    ui->actionToolButton_8->setEnabled(false);
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_8_triggered()
{
  ui->actionToolButton_1->setChecked(false);
  ui->actionToolButton_2->setChecked(false);
  ui->actionToolButton_3->setChecked(false);
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(false);
  ui->actionToolButton_6->setChecked(false);
  // set user action
  ui->widget_2->setUserOperation( 7, Qt::ArrowCursor );
  if( ui->widget_2->hasUndoing() )
    ui->actionToolButton_7->setEnabled(true);
  else
    ui->actionToolButton_7->setEnabled(false);
  if( ui->widget_2->hasRedoing() )
    ui->actionToolButton_8->setEnabled(true);
  else
    ui->actionToolButton_8->setEnabled(false);
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_9_triggered()
{
  // set user action
  Ui::MeshBoundary dlg; 
  QDialog *dialog = new QDialog;
  dlg.setupUi(dialog);
  int n1 = 0;
  int n2 = 0;
  Model* pModel = ui->widget_2->getModel();
  pModel->getNumSubdivOfSelectedBoundary( n1, n2 );
  QString sn1;
  sn1.setNum( n1 );
  dlg.lineEdit->clear();
  dlg.lineEdit->insert( sn1 );
  if( dialog->exec() == QDialog::Accepted )
  {
    sn1 = dlg.lineEdit->text();
    pModel->setNumSubdivOfSelectedBoundary( sn1.toInt(), n2 );
    // returns to the selection mode
    this->on_actionToolButton_2_triggered();
    this->on_actionToolButton_1_triggered();
  }
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_10_triggered()
{
  // set user action
  Ui::MeshDomain dlg; 
  QDialog *dialog = new QDialog;
  dlg.setupUi(dialog);
  if( dialog->exec() == QDialog::Accepted )
  {
	  int meshGenType=0; // 1: Delunary 2: Advancing Front
	  if( dlg.radioButton->isChecked ())
	  {
		  meshGenType=1;
	  }
	  else
		  meshGenType = 2;
	  Model* pModel = ui->widget_2->getModel();
	  QString ss = dlg.lineEdit->text();
      int teste = ss.toInt();

	  pModel->generateMeshOfSelectedDomain(meshGenType,teste);
	  // returns to the selection mode
	  this->on_actionToolButton_2_triggered();
	  this->on_actionToolButton_1_triggered();
  }
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_11_triggered()
{
  // verifies if the grid button is turned on or off
  bool test = ui->actionToolButton_11->isChecked();
  // get data from grid/snap and set in the dialog
  double dx = 0.0;
  double dy = 0.0;
  bool isSnapOn = ui->widget_2->getGridSnapInfo( dx, dy );
  QString sdx;
  sdx.setNum( dx );
  m_gridDlg.lineEdit->clear();
  m_gridDlg.lineEdit->insert( sdx );
  QString sdy;
  sdy.setNum( dy );
  m_gridDlg.lineEdit_2->clear();
  m_gridDlg.lineEdit_2->insert( sdy );
  m_gridDlg.checkBox->setChecked(isSnapOn);
  if( test )
  {
    // opens dialog and get info from grid and snap
    if( m_gridDialog->exec() == QDialog::Accepted )
    {
      // set info to activate grid and snap
      sdx = m_gridDlg.lineEdit->text();
      sdy = m_gridDlg.lineEdit_2->text();
      isSnapOn = m_gridDlg.checkBox->isChecked();
      ui->widget_2->setGridSnapData( test, isSnapOn, sdx.toDouble(), sdy.toDouble() );
    }
    else
    {
      ui->actionToolButton_11->setChecked( false );
    }
  }
  else
  {
    // set info to deactivate grid and snap
    ui->widget_2->setGridSnapData( test, isSnapOn, dx, dy  );
  }
  // returns to the selection mode
  this->on_actionToolButton_2_triggered();
  this->on_actionToolButton_1_triggered();
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_12_triggered()
{
  // opens attribute manager dialog
  m_attrDialog->show();
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_13_triggered()
{
  // sets mesh definition
  // verifies if the mesh button is turned on or off
  Model* pModel = ui->widget_2->getModel();
  bool test = ui->actionToolButton_13->isChecked();
  if( test )  
  {
    // generate mesh
    pModel->generateMesh();
    // disable all buttons
    ui->actionToolButton_4->setEnabled(false);
    ui->actionToolButton_5->setEnabled(false);
    ui->actionToolButton_6->setEnabled(false);
    ui->actionToolButton_7->setEnabled(false);
    ui->actionToolButton_8->setEnabled(false);
    ui->actionToolButton_9->setEnabled(false);
    ui->actionToolButton_10->setEnabled(false);
    ui->actionToolButton_12->setEnabled(false);
  }
  else
  {
    pModel->cleanMesh();
    ui->actionToolButton_4->setEnabled(true);
    ui->actionToolButton_5->setEnabled(true);
    ui->actionToolButton_6->setEnabled(true);
    ui->actionToolButton_7->setEnabled(true);
    ui->actionToolButton_8->setEnabled(true);
    ui->actionToolButton_9->setEnabled(true);
    ui->actionToolButton_10->setEnabled(true);
    ui->actionToolButton_12->setEnabled(true);
  }
  // and returns to the selection mode
  this->on_actionToolButton_2_triggered();
  this->on_actionToolButton_1_triggered();
}
//-------------------------------------------------------------------------
void MyGUI::on_actionToolButton_14_triggered()
{
  ui->actionToolButton_4->setChecked(false);
  ui->actionToolButton_5->setChecked(false);      
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
// Events from buttons of the Attribute Manager DIALOG
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::applyAttributeClicked()
{
  // checks for selected entity and for compatibility with the attribute
  Model* pModel = ui->widget_2->getModel();
  if( pModel != NULL )
  {
    QTreeWidgetItem* itemTree = m_attrDlg.treeWidget->currentItem();
    QString	itext = itemTree->text( 0 );
    Attribute* attrib = m_attribManager->getAttributeByName( itext.toLocal8Bit().constData() );
    if( attrib->isApplicableOnEdge() )
    {
      pModel->applyAttributeOnSelectedBoundary( attrib );
      // returns to the selection mode
      this->on_actionToolButton_2_triggered();
      this->on_actionToolButton_1_triggered();
    }
    if( attrib->isApplicableOnFace() )
    {
      pModel->applyAttributeOnSelectedDomain( attrib );
      // returns to the selection mode
      this->on_actionToolButton_2_triggered();
      this->on_actionToolButton_1_triggered();
    }
    if( attrib->isApplicableOnVertex() )
    {
      pModel->applyAttributeOnSelectedVertex( attrib );
      // returns to the selection mode
      this->on_actionToolButton_2_triggered();
      this->on_actionToolButton_1_triggered();
    }
  }
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::removeAttributeClicked()
{
  // checks for selected entity and for compatibility with the attribute
  Model* pModel = ui->widget_2->getModel();
  if( pModel != NULL )
  {
    QTreeWidgetItem* itemTree = m_attrDlg.treeWidget->currentItem();
    QString	itext = itemTree->text( 0 );
    Attribute* attrib = m_attribManager->getAttributeByName( itext.toLocal8Bit().constData() );
    if( attrib->isApplicableOnEdge() )
    {
      pModel->applyAttributeOnSelectedBoundary( NULL );
      // returns to the selection mode
      this->on_actionToolButton_2_triggered();
      this->on_actionToolButton_1_triggered();
    }
    if( attrib->isApplicableOnFace() )
    {
      pModel->applyAttributeOnSelectedDomain( NULL );
      // returns to the selection mode
      this->on_actionToolButton_2_triggered();
      this->on_actionToolButton_1_triggered();
    }
  }
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void MyGUI::cancelAttributeClicked()
{
  m_attrDialog->hide();
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
void MyGUI::createAttributeClicked()
{
  int index = m_attrDlg.comboBox->currentIndex();
  QString text = m_attrDlg.comboBox->currentText();
  string name = m_attribManager->createAttributeFromPrototype( index );
  // creates a new attribute item in the tree
  QString ss(name.c_str()); 
  QTreeWidgetItem* item = new QTreeWidgetItem();
  item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);
  item->setText(0,ss);
  m_attrDlg.treeWidget->insertTopLevelItem(0,item);
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void 
MyGUI::displayAttribProperties()
{
  // clear the contents of the property table
  m_attrDlg.tableWidget->clearContents();
  m_attrDlg.tableWidget->setRowCount(0);
  // get info from the selected item on the tree:
  QTreeWidgetItem* item = m_attrDlg.treeWidget->currentItem();
  QString	itext = item->text( 0 );
  Attribute* attrib = m_attribManager->getAttributeByName( itext.toLocal8Bit().constData() );
  map<string,string> prop = attrib->getProperties();
  map<string,string>::iterator it;
  for( it=prop.begin(); it!=prop.end(); it++ )
  {
     int row = m_attrDlg.tableWidget->rowCount();
     m_attrDlg.tableWidget->insertRow(row);
     QTableWidgetItem *sizeItem1 = new QTableWidgetItem;
     sizeItem1->setText( QString(it->first.c_str()) );
     QTableWidgetItem *sizeItem2 = new QTableWidgetItem;
     sizeItem2->setText( QString(it->second.c_str()) );
     m_attrDlg.tableWidget->setItem(row, 0, sizeItem1);
     m_attrDlg.tableWidget->setItem(row, 1, sizeItem2);
  }
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void 
MyGUI::updateAttributeValues( int _r, int _c )
{
  if( _r!=0 || _c!=0 )
  {
    QTableWidgetItem*	itemT1 = m_attrDlg.tableWidget->item( _r, _c-1 );
    QTableWidgetItem*	itemT2 = m_attrDlg.tableWidget->item( _r, _c );
    QString	itextT1 = itemT1->text();
    QString	itextT2 = itemT2->text();
    QTreeWidgetItem* itemTree = m_attrDlg.treeWidget->currentItem();
    QString	itext = itemTree->text( 0 );
    Attribute* attrib = m_attribManager->getAttributeByName( itext.toLocal8Bit().constData() );
    bool status = attrib->setPropertyValues( itextT1.toLocal8Bit().constData(), itextT2.toLocal8Bit().constData() );
  }
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void 
MyGUI::updateAtribSymbolSize( int _s )
{
  double val = pow((double)_s/4.0,2.0);
  // just modify the scale factor to render the attribute symbols and redraw
  Model* pModel = ui->widget_2->getModel();
  if( pModel != NULL )
  {
    pModel->scaleAttributeSymbol( val );
    // returns to the selection mode
    this->on_actionToolButton_2_triggered();
    this->on_actionToolButton_1_triggered();
  }
}
//-------------------------------------------------------------------------

void MyGUI::okInsertPointClicked()
{
	double dx, dy, dtol;
	
	dx = m_insertPnt.lineEdit->text().toDouble();
	dy =  m_insertPnt.lineEdit_2->text().toDouble();
	dtol =  m_insertPnt.lineEdit_3->text().toDouble();

	if (dtol < 0.16)
	{
       dtol = 0.16;
	}
	
	ui->widget_2->createPoint(dx,dy,dtol);
    m_insertPoint->hide();
}
//-------------------------------------------------------------------------
void MyGUI::cancelInsertPointClicked()
{
	m_insertPoint->hide();
}
//-------------------------------------------------------------------------
void MyGUI::applyInsertPointClicked()
{
	double dx, dy, dtol;
	
	dx = m_insertPnt.lineEdit->text().toDouble();
	dy =  m_insertPnt.lineEdit_2->text().toDouble();
	dtol =  m_insertPnt.lineEdit_3->text().toDouble();

	if (dtol < 0.16)
	{
       dtol = 0.16;
	}
	
	ui->widget_2->createPoint(dx,dy,dtol);

	m_insertPnt.lineEdit->  clear();
	m_insertPnt.lineEdit_2->clear();
	// returns to the selection mode
    this->on_actionToolButton_2_triggered();
    this->on_actionToolButton_1_triggered();
}
//-------------------------------------------------------------------------
void MyGUI::okInsertLineClicked() 
{
	double dx1, dx2, dy1, dy2, dtol;
	
	dx1 = m_insertLn.lineEdit->text().toDouble();
	dx2 = m_insertLn.lineEdit_2->text().toDouble();
	dy1 = m_insertLn.lineEdit_3->text().toDouble();
	dy2 = m_insertLn.lineEdit_4->text().toDouble();
	dtol = m_insertLn.lineEdit_5->text().toDouble();

	if (dtol < 0.16)
	{
       dtol = 0.16;
	}

	testprintLine(dx1, dx2, dy1, dy2, dtol);
	m_insertLine->hide();
}
//-------------------------------------------------------------------------
void MyGUI::cancelInsertLineClicked()
{
	m_insertLine->hide();
}
//-------------------------------------------------------------------------
void MyGUI::applyInsertLineClicked()
{
	double dx1, dx2, dy1, dy2, dtol;
	
	dx1 = m_insertLn.lineEdit->text().toDouble();
	dx2 = m_insertLn.lineEdit_2->text().toDouble();
	dy1 = m_insertLn.lineEdit_3->text().toDouble();
	dy2 = m_insertLn.lineEdit_4->text().toDouble();
	dtol = m_insertLn.lineEdit_5->text().toDouble();

	if (dtol < 0.16)
	{
       dtol = 0.16;
	}

	testprintLine(dx1, dx2, dy1, dy2, dtol);

    m_insertLn.lineEdit->  clear();
	m_insertLn.lineEdit_2->clear();
	m_insertLn.lineEdit_3->clear();
	m_insertLn.lineEdit_4->clear();
	// returns to the selection mode
    this->on_actionToolButton_2_triggered();
    this->on_actionToolButton_1_triggered();
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void MyGUI::okInsertRecClicked() 
{
	
	double dx1, dx2, dy1, dy2, dtol;
	
	dx1  = m_insertRc.lineEdit->text().toDouble();
	dx2  = m_insertRc.lineEdit_2->text().toDouble();
	dy1  = m_insertRc.lineEdit_3->text().toDouble();
	dy2  = m_insertRc.lineEdit_4->text().toDouble();
	dtol = m_insertRc.lineEdit_5->text().toDouble();

	if (dtol < 0.16)
	{
       dtol = 0.16;
	}

	testprintRec(dx1, dx2, dy1, dy2, dtol);
	m_insertRec->hide();
}
//-------------------------------------------------------------------------
void MyGUI::cancelInsertRecClicked()
{
	m_insertRec->hide();
}
//-------------------------------------------------------------------------
void MyGUI::applyInsertRecClicked()
{
	double dx1, dx2, dy1, dy2, dtol;	
	
	dx1  = m_insertRc.lineEdit->text().toDouble();
	dx2  = m_insertRc.lineEdit_2->text().toDouble();
	dy1  = m_insertRc.lineEdit_3->text().toDouble();
	dy2  = m_insertRc.lineEdit_4->text().toDouble();
	dtol = m_insertRc.lineEdit_5->text().toDouble();

	if (dtol < 0.16)
	{
       dtol = 0.16;
	}

	testprintRec(dx1, dx2, dy1, dy2, dtol);

	m_insertRc.lineEdit->  clear();
	m_insertRc.lineEdit_2->clear();
	m_insertRc.lineEdit_3->clear();
	m_insertRc.lineEdit_4->clear();
	// returns to the selection mode
    this->on_actionToolButton_2_triggered();
    this->on_actionToolButton_1_triggered();
}
//-------------------------------------------------------------------------
void MyGUI::testprintRec(double _x1, double _x2, double _y1, double _y2, double _tol)
{
   double dist = pow((_x1 - _x2)*(_x1 - _x2) + (_y1 - _y2)*(_y1 - _y2),(0.5));
   bool check1 = false, check2 = false;
 
   if (dist > _tol)
   {
             if (abs(_x1 - _x2) < _tol)
			 {
				 ui->widget_2->createLine(_x1, _x1, _y1, _y2, _tol);
			 }
			 else
			 {
				 check1 = true; //artificio para nao construir um retangulo em cima do outro
			 }
			 if ( abs(_y1 - _y2) < _tol)
			 {
				 ui->widget_2->createLine(_x1, _x2, _y1, _y1, _tol);
			 }
			 else
			 {
				 check2 = true;  	
			 }
			 if (check1 == check2)
			 {
				ui->widget_2->createLine(_x1, _x2, _y1, _y1, _tol);
				ui->widget_2->createLine(_x2, _x2, _y1, _y2, _tol);
				ui->widget_2->createLine(_x1, _x2, _y2, _y2, _tol);
				ui->widget_2->createLine(_x1, _x1, _y1, _y2, _tol);
			 }
   }
   else 
   {
		ui->widget_2->createPoint(_x1,_y1,_tol);	 		      
   }       
}
//-------------------------------------------------------------------------
void MyGUI::testprintLine(double _x1, double _x2, double _y1, double _y2, double _tol)
{
   double dist = pow((_x1 - _x2)*(_x1 - _x2) + (_y1 - _y2)*(_y1 - _y2),(0.5)); 

   if (dist < _tol)
   {
      ui->widget_2->createPoint(_x1,_y1,_tol);	  
   }
   else 
   {
      ui->widget_2->createLine(_x1, _x2, _y1, _y2, _tol);
   }       
}
//-------------------------------------------------------------------------
